package com.insyte.activities;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.insyte.R;
import com.insyte.adapters.ProfilePagerAdapter;
import com.insyte.config.Constants;
import com.insyte.config.InsyteApp;
import com.insyte.model.User;
import com.insyte.utils.Initialize;
import com.insyte.utils.NonSwipeableViewPager;

import org.json.JSONObject;

public class ProfileActivity extends FragmentActivity {

    private NonSwipeableViewPager viewPager;
    private ProfilePagerAdapter profilePagerAdapter;
    public static int currentPage = 0;
    private String url_get_profile = Constants.URL_API + "/user/profile/%s?token=%s";
    private User user;
    private AQuery aQuery;
    private Initialize initialize;
    private ProgressDialog progressDialog;
    public static String gender = "",story = "",dating_status = "";
    public static int age = 0,height = 0;
    private String url_pull_interests_fb = "https://graph.facebook.com/v2.3/me/likes?access_token=%s";
    private SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        prefs = this.getSharedPreferences(Constants.PREFS_NAME, Context.MODE_PRIVATE);
        getView();
        setEvent();
        user = InsyteApp.getInstance().getUser();
        Log.e("user_id", user.getId());
        Log.e("user_token",user.getToken());
        initialize = new Initialize(this);
        aQuery = new AQuery(this);
        //progressDialog = initialize.createProgressDialog("Loading");
        //updateUserProfile();

    }

    private void getView() {
        viewPager = (NonSwipeableViewPager) findViewById(R.id.viewPager);
        profilePagerAdapter = new ProfilePagerAdapter(getSupportFragmentManager(), viewPager);
        viewPager.setAdapter(profilePagerAdapter);
        viewPager.setPagingEnabled(false);
    }

    private void setEvent() {

    }

    private void updateUserProfile() {
        progressDialog.show();
        String url_get_userprofile = String.format(url_get_profile,user.getId(),user.getToken());
        if (initialize.checkNetwork()) {
            aQuery.ajax(url_get_userprofile,String.class,new AjaxCallback<String>() {
                @Override
                public void callback(String url, String result, AjaxStatus status) {
                    progressDialog.dismiss();
                    if(result !=null) {
                        try {
                            JSONObject js = new JSONObject(result);
                            if (js.has("code")) {
                                if (js.getInt("code") == 1) {
                                    JSONObject jsonObject = js.getJSONObject("data");
                                    gender = jsonObject.getString("gender");
                                    story = jsonObject.getString("story");
                                    dating_status = jsonObject.getString("dating_status");
                                    age = jsonObject.getInt("age");
                                    height = jsonObject.getInt("height");
                                    if (age > 0) {
                                        user.setAge(age +"");
                                    }
                                    if(height > 0) {
                                        user.setHeight(height +"");
                                    }
                                    if(gender.equalsIgnoreCase("unknown")) {
                                        user.setGender("male");
                                    }
                                    else {
                                        user.setGender(gender);
                                    }
                                    if(story.length() > 0) {
                                        user.setStory(story);
                                    }
                                    if(dating_status.length() > 0) {
                                        user.setDating_status(dating_status);
                                    }
                                    InsyteApp.getInstance().saveUser(user);
                                } else {
                                    initialize.showToast(js.getString("message"));
                                }
                            }
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        else {
            initialize.showToast("Please check your network!");
        }
    }


//    private NonSwipeableViewPager viewPager;
//    private ProfilePagerAdapter profilePagerAdapter;
//    public static int currentPage = 0;
//    private String url_get_profile = Constants.URL_API + "/user/profile/%s?token=%s";
//    private User user;
//    private AQuery aQuery;
//    private Initialize initialize;
//    private ProgressDialog progressDialog;
//    public static String gender = "",story = "",dating_status = "";
//    public static int age = 0,height = 0;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_profile);
//        getView();
//        setEvent();
//        user = InsyteApp.getInstance().getUser();
//        Log.e("user_id", user.getId());
//        Log.e("user_token",user.getToken());
//        initialize = new Initialize(this);
//        aQuery = new AQuery(this);
//        progressDialog = initialize.createProgressDialog("Loading");
//        updateUserProfile();
//    }
//
//    private void getView() {
//        viewPager = (NonSwipeableViewPager) findViewById(R.id.viewPager);
//        profilePagerAdapter = new ProfilePagerAdapter(getSupportFragmentManager(), viewPager);
//        viewPager.setAdapter(profilePagerAdapter);
//        viewPager.setPagingEnabled(false);
//    }
//
//    private void setEvent() {
//
//    }
//
//    private void updateUserProfile() {
//        progressDialog.show();
//        String url_get_userprofile = String.format(url_get_profile,user.getId(),user.getToken());
//        if (initialize.checkNetwork()) {
//            aQuery.ajax(url_get_userprofile,String.class,new AjaxCallback<String>() {
//                @Override
//                public void callback(String url, String result, AjaxStatus status) {
//                    progressDialog.dismiss();
//                    if(result !=null) {
//                        try {
//                            JSONObject js = new JSONObject(result);
//                            if (js.has("code")) {
//                                if (js.getInt("code") == 1) {
//                                    JSONObject jsonObject = js.getJSONObject("data");
//                                    gender = jsonObject.getString("gender");
//                                    story = jsonObject.getString("story");
//                                    dating_status = jsonObject.getString("dating_status");
//                                    age = jsonObject.getInt("age");
//                                    height = jsonObject.getInt("height");
//                                    if (age > 0) {
//                                        user.setAge(age +"");
//                                    }
//                                    if(height > 0) {
//                                        user.setHeight(height +"");
//                                    }
//                                    if(gender.equalsIgnoreCase("unknown")) {
//                                        user.setGender("male");
//                                    }
//                                    else {
//                                        user.setGender(gender);
//                                    }
//                                    if(story.length() > 0) {
//                                        user.setStory(story);
//                                    }
//                                    if(dating_status.length() > 0) {
//                                        user.setDating_status(dating_status);
//                                    }
//                                    InsyteApp.getInstance().saveUser(user);
//                                } else {
//                                    initialize.showToast(js.getString("message"));
//                                }
//                            }
//                        }
//                        catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            });
//        }
//        else {
//            initialize.showToast("Please check your network!");
//        }
//    }

}
